package com.wzz.controller;

import com.wzz.pojo.Goods;
import com.wzz.pojo.Indent;
import com.wzz.pojo.User;
import com.wzz.service.GoodsService;
import com.wzz.service.IndentService;
import com.wzz.service.UserService;
import com.wzz.vo.PageInfoVo;
import com.wzz.vo.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import jdk.nashorn.internal.parser.Token;
import org.elasticsearch.cluster.routing.allocation.RerouteExplanation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author DarkClown
 * @date 2024-08-12 10:38
 * @project_name day0812-month
 * @parem
 **/
@RestController
@RequestMapping("/user/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @Autowired
    IndentService indentService;

    /**
     * 查询列表
     * @param pageInfoVo
     * @return
     */
    @RequestMapping("selectList")
    private R selectList(@RequestBody PageInfoVo pageInfoVo){
        return goodsService.selectList(pageInfoVo);
    }
    /**
     * 查询三级联动
     */
    @RequestMapping("selectType/{id}")
    private R selectType(@PathVariable("id")Integer id){
        return goodsService.selectType(id);
    }

    /**
     * 图片上传
     */
    @RequestMapping("/uploadFile")
    private R uploadFile(MultipartFile file){
        return goodsService.uploadFile(file);
    }

    @RequestMapping("/addGoods")
    private R addGoods(@RequestBody Goods goods){
        return goodsService.addGoods(goods);
    }

    @Autowired
    HttpServletRequest request;


    @RequestMapping("/updateMystatus")
    private R updateMyStatus(@RequestBody Goods goods){
        String token = request.getHeader("token");
        System.out.println(token);
        return goodsService.updateMyStatus(goods,token);

    }

    @RequestMapping("/selectGoods/{id}")
    private R selectGoods(@PathVariable("id")Integer id){
        return goodsService.selectId(id);
    }

    @Autowired
    UserService userService;

    @RequestMapping("insertIndent")
    private R insertIndent(@RequestBody Indent indent){
        String token = request.getHeader("token");
        Claims token1 = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        Integer uid = token1.get("uid", Integer.class);

        User byId = userService.getById(uid);

        if (byId.getPoints()<=indent.getGid()){
            return R.ERROR(404,"积分不足");
        }

        indent.setBuyTime(new Date());

         indentService.save(indent);
        return R.OK();
    }
}
